import {
  ISoftware,
  ISoftwareVersion,
  ISoftwareVulnerability,
  ISoftwareTitleVersion,
  ISoftwarePackage,
  ISoftwareTitle,
  ISoftwareTitleDetails,
  IAppStoreApp,
  IFleetMaintainedApp,
  IFleetMaintainedAppDetails,
  ISoftwareInstallResult,
} from "interfaces/software";
import {
  ISoftwareTitlesResponse,
  ISoftwareTitleResponse,
  ISoftwareVersionsResponse,
  ISoftwareVersionResponse,
} from "services/entities/software";
import { IOSVersionsResponse } from "../services/entities/operating_systems";
import { IGetSetupExperienceSoftwareResponse } from "../services/entities/mdm";
import { IOperatingSystemVersion } from "../interfaces/operating_system";
import { createMockListEntitiesResponseCommon } from "./commonMock";

const DEFAULT_SOFTWARE_MOCK: ISoftware = {
  hosts_count: 1,
  id: 1,
  name: "mock software 1.app",
  version: "1.0.0",
  source: "apps",
  generated_cpe: "",
  vulnerabilities: null,
  last_opened_at: null,
  bundle_identifier: "com.app.mock",
  icon_url: null,
};

export const createMockSoftware = (
  overrides?: Partial<ISoftware>
): ISoftware => {
  return { ...DEFAULT_SOFTWARE_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_TITLE_VERSION_MOCK = {
  id: 1,
  version: "1.0.0",
  vulnerabilities: ["CVE-2020-0001"],
};

export const createMockSoftwareTitleVersion = (
  overrides?: Partial<ISoftwareTitleVersion>
): ISoftwareTitleVersion => {
  return { ...DEFAULT_SOFTWARE_TITLE_VERSION_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_VULNERABILITY_MOCK = {
  cve: "CVE-2020-0001",
  details_link: "https://test.com",
  cvss_score: 9,
  epss_probability: 0.8,
  cisa_known_exploit: false,
  cve_published: "2020-01-01T00:00:00.000Z",
  cve_description: "test description",
  resolved_in_version: "1.2.3",
};

export const createMockSoftwareVulnerability = (
  overrides?: Partial<ISoftwareVulnerability>
): ISoftwareVulnerability => {
  return { ...DEFAULT_SOFTWARE_VULNERABILITY_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_VERSION_MOCK: ISoftwareVersion = {
  id: 1,
  name: "test.app",
  display_name: "Test App",
  version: "1.2.3",
  bundle_identifier: "com.test.Desktop",
  source: "apps",
  extension_for: "chrome",
  release: "1",
  vendor: "test_vendor",
  arch: "x86_64",
  generated_cpe: "cpe:test:app:1.2.3",
  vulnerabilities: [createMockSoftwareVulnerability()],
  hosts_count: 1,
};

export const createMockSoftwareVersion = (
  overrides?: Partial<ISoftwareVersion>
): ISoftwareVersion => {
  return { ...DEFAULT_SOFTWARE_VERSION_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_VERSIONS_RESPONSE_MOCK: ISoftwareVersionsResponse = {
  counts_updated_at: "2020-01-01T00:00:00.000Z",
  count: 1,
  software: [createMockSoftwareVersion()],
  meta: {
    has_next_results: false,
    has_previous_results: false,
  },
};

export const createMockSoftwareVersionsResponse = (
  overrides?: Partial<ISoftwareVersionsResponse>
): ISoftwareVersionsResponse => {
  return { ...DEFAULT_SOFTWARE_VERSIONS_RESPONSE_MOCK, ...overrides };
};

const DEFAULT_OS_VERSION_MOCK = {
  os_version_id: 1,
  name: "macOS 14.6.1",
  name_only: "macOS",
  version: "14.6.1",
  platform: "darwin",
  hosts_count: 42,
  generated_cpes: [],
  kernels: [],
  vulnerabilities: [],
};

export const createMockOSVersion = (
  overrides?: Partial<IOperatingSystemVersion>
): IOperatingSystemVersion => {
  return {
    ...DEFAULT_OS_VERSION_MOCK,
    ...overrides,
  };
};

const DEFAULT_OS_VERSIONS_RESPONSE_MOCK: IOSVersionsResponse = {
  counts_updated_at: "2020-01-01T00:00:00.000Z",
  count: 1,
  os_versions: [createMockOSVersion()],
  meta: {
    has_next_results: false,
    has_previous_results: false,
  },
};

export const createMockOSVersionsResponse = (
  overrides?: Partial<IOSVersionsResponse>
): IOSVersionsResponse => {
  return { ...DEFAULT_OS_VERSIONS_RESPONSE_MOCK, ...overrides };
};

const DEFAULT_APP_STORE_APP_MOCK: IAppStoreApp = {
  name: "test app",
  app_store_id: "1",
  created_at: "2020-01-01T00:00:00.000Z",
  platform: "darwin",
  icon_url: "https://via.placeholder.com/512",
  latest_version: "1.2.3",
  self_service: true,
  status: {
    installed: 1,
    pending: 2,
    failed: 3,
  },
  labels_include_any: null,
  labels_exclude_any: null,
};

export const createMockAppStoreApp = (overrides?: Partial<IAppStoreApp>) => {
  return { ...DEFAULT_APP_STORE_APP_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_TITLE_DETAILS_MOCK: ISoftwareTitleDetails = {
  id: 1,
  name: "test.app",
  icon_url: null,
  software_package: null,
  app_store_app: null,
  source: "apps",
  hosts_count: 1,
  versions: [createMockSoftwareTitleVersion()],
  bundle_identifier: "com.test.Desktop",
  versions_count: 1,
  counts_updated_at: "2024-11-01T01:23:45Z",
};

export const createMockSoftwareTitleDetails = (
  overrides?: Partial<ISoftwareTitleDetails>
) => {
  return { ...DEFAULT_SOFTWARE_TITLE_DETAILS_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_TITLE_RESPONSE: ISoftwareTitleResponse = {
  software_title: createMockSoftwareTitleDetails(),
};

export const createMockSoftwareTitleResponse = (
  overrides?: Partial<ISoftwareTitleResponse>
): ISoftwareTitleResponse => {
  return { ...DEFAULT_SOFTWARE_TITLE_RESPONSE, ...overrides };
};

const DEFAULT_SOFTWARE_VERSION_RESPONSE = {
  software: createMockSoftwareVersion(),
};

export const createMockSoftwareVersionResponse = (
  overrides?: Partial<ISoftwareVersionResponse>
): ISoftwareVersionResponse => {
  return { ...DEFAULT_SOFTWARE_VERSION_RESPONSE, ...overrides };
};

const DEFAULT_SOFTWARE_PACKAGE_MOCK: ISoftwarePackage = {
  name: "TestPackage-1.2.3.pkg",
  title_id: 2,
  version: "1.2.3",
  uploaded_at: "2020-01-01T00:00:00.000Z",
  install_script: "sudo installer -pkg /temp/FalconSensor-6.44.pkg -target /",
  uninstall_script: "sudo rm -rf /Applications/Falcon.app",
  pre_install_query: "SELECT 1 FROM macos_profiles WHERE uuid='abc123';",
  post_install_script:
    "sudo /Applications/Falcon.app/Contents/Resources/falconctl license abc123",
  self_service: false,
  icon_url: null,
  display_name: "",
  status: {
    installed: 1,
    pending_install: 2,
    failed_install: 1,
    pending_uninstall: 1,
    failed_uninstall: 1,
  },
  automatic_install_policies: [],
  url: "https://fakeurl.testpackageurlforfalconapp.fake/test/package",
  hash_sha256: "abcd1234",
  labels_include_any: null,
  labels_exclude_any: null,
  install_during_setup: undefined,
};

export const createMockSoftwarePackage = (
  overrides?: Partial<ISoftwarePackage>
) => {
  return { ...DEFAULT_SOFTWARE_PACKAGE_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_TITLE_MOCK: ISoftwareTitle = {
  id: 1,
  name: "mock software 1.app",
  icon_url: null,
  versions_count: 1,
  source: "apps",
  hosts_count: 1,
  extension_for: "",
  versions: [createMockSoftwareTitleVersion()],
  software_package: createMockSoftwarePackage(),
  app_store_app: null,
};

export const createMockSoftwareTitle = (
  overrides?: Partial<ISoftwareTitle>
): ISoftwareTitle => {
  return {
    ...DEFAULT_SOFTWARE_TITLE_MOCK,
    ...overrides,
  };
};

const DEFAULT_SOFTWARE_TITLES_RESPONSE_MOCK: ISoftwareTitlesResponse = {
  counts_updated_at: "2020-01-01T00:00:00.000Z",
  count: 1,
  software_titles: [createMockSoftwareTitle()],
  meta: {
    has_next_results: false,
    has_previous_results: false,
  },
};

export const createMockSoftwareTitlesResponse = (
  overrides?: Partial<ISoftwareTitlesResponse>
): ISoftwareTitlesResponse => {
  return { ...DEFAULT_SOFTWARE_TITLES_RESPONSE_MOCK, ...overrides };
};

const DEFAULT_FLEET_MAINTAINED_APPS_MOCK: IFleetMaintainedApp = {
  id: 1,
  name: "test app",
  version: "1.2.3",
  platform: "darwin",
};

export const createMockFleetMaintainedApp = (
  overrides?: Partial<IFleetMaintainedApp>
): IFleetMaintainedApp => {
  return {
    ...DEFAULT_FLEET_MAINTAINED_APPS_MOCK,
    ...overrides,
  };
};

const DEFAULT_FLEET_MAINTAINED_APP_DETAILS_MOCK: IFleetMaintainedAppDetails = {
  id: 1,
  name: "Test app",
  version: "1.2.3",
  platform: "darwin",
  pre_install_script: "SELECT * FROM osquery_info WHERE start_time > 1",
  install_script: '#!/bin/sh\n\ninstaller -pkg "$INSTALLER" -target /',
  post_install_script: 'echo "Installed"',
  uninstall_script:
    "#!/bin/sh\n\n# Fleet extracts and saves package IDs\npkg_ids=$PACKAGE_ID",
  slug: "applications/test-app",
  url: "http://www.testurl1234abcd.com/testapp",
  categories: ["Browsers"],
};

export const createMockFleetMaintainedAppDetails = (
  overrides?: Partial<IFleetMaintainedAppDetails>
) => {
  return { ...DEFAULT_FLEET_MAINTAINED_APP_DETAILS_MOCK, ...overrides };
};

const DEFAULT_SOFTWARE_INSTALL_RESULT: ISoftwareInstallResult = {
  host_display_name: "Test Host",
  install_uuid: "uuid-123",
  software_title: "CoolApp",
  software_title_id: 1,
  software_package: "com.cool.app",
  host_id: 42,
  status: "installed",
  detail: "",
  output: "",
  pre_install_query_output: "",
  post_install_script_output: "",
  created_at: "2025-08-10T12:00:00Z",
  updated_at: "2025-08-10T12:05:00Z",
  self_service: false,
};

export const createMockSoftwareInstallResult = (
  overrides?: Partial<ISoftwareInstallResult>
) => {
  return {
    ...DEFAULT_SOFTWARE_INSTALL_RESULT,
    ...overrides,
  };
};

const DEFAULT_SETUP_EXPERIENCE_SOFTWARE_MOCK: IGetSetupExperienceSoftwareResponse = {
  ...createMockListEntitiesResponseCommon(),
  counts_updated_at: null,
  software_titles: [createMockSoftwareTitle()],
};

export const createMockSetupExperienceSoftware = (
  overrides?: Partial<IGetSetupExperienceSoftwareResponse>
): IGetSetupExperienceSoftwareResponse => {
  return { ...DEFAULT_SETUP_EXPERIENCE_SOFTWARE_MOCK, ...overrides };
};
